package cn.jhz.learn.blog.controller.security;

import cn.jhz.learn.blog.common.bean.JsonData;
import cn.jhz.learn.blog.service.security.permission.UserService;
import cn.jhz.learn.blog.vo.securiity.Login;
import cn.jhz.learn.blog.vo.securiity.Register;

import org.jose4j.lang.JoseException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@RestController
@RequestMapping("/api/public/")
public class LoginController {

    private final UserService userService;

    @Autowired
    public LoginController(UserService userService){
        this.userService = userService;
    }


    @PostMapping(path = "login", consumes = "application/json" )
    public JsonData Login(@RequestBody @Validated Login userInfo, HttpServletResponse reponse) throws JoseException {


        //TODO:验证码校验
        userService.addOnlineUser(userInfo, reponse);
        return JsonData.success("登陆成功");
    }

    @PostMapping(path = "register", consumes = "application/json")
    public JsonData register(@RequestBody Register userInfo){


        //TODO:验证码校验
        userService.addUser(userInfo);
        return JsonData.success("注册成功");
    }

    @GetMapping("echo")
    public void register(HttpServletRequest servletRequest, HttpServletResponse servletResponse){
        //判断当前token是否过期,并更新token,若过期抛异常
    }
}
